﻿using B.W.passport.Application.DTO;
using B.W.passport.Domain;
using B.W.passport.IBaseRepisitory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.W.passport.Application.AdminService
{
    public class AdminService:IAdminService
    {
        /// <summary>
        /// 仓储-用户
        /// </summary>
        private readonly IBaseRepisitory<Account> AccountRepisitory;
        /// <summary>
        /// 仓储-角色
        /// </summary>
        private readonly IBaseRepisitory<Role> RoleRepisitory;
        /// <summary>
        /// 仓储-用户角色
        /// </summary>
        private readonly IBaseRepisitory<AccountRole> AccountRoleRepisitory;
        /// <summary>
        /// 仓储-权限
        /// </summary>
        private readonly IBaseRepisitory<Permission> PermissionRepisitory;
        /// <summary>
        /// 仓储-角色权限
        /// </summary>
        private readonly IBaseRepisitory<RolePermission> RolePermissionRepisitory;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="accountRepisitory"></param>
        /// <param name="roleRepisitory"></param>
        /// <param name="accountRoleRepisitory"></param>
        /// <param name="permissionRepisitory"></param>
        /// <param name="rolePermissionRepisitory"></param>
        public AdminService(IBaseRepisitory<Account> accountRepisitory, IBaseRepisitory<Role> roleRepisitory, IBaseRepisitory<AccountRole> accountRoleRepisitory, IBaseRepisitory<Permission> permissionRepisitory, IBaseRepisitory<RolePermission> rolePermissionRepisitory)
        {
            AccountRepisitory = accountRepisitory;
            RoleRepisitory = roleRepisitory;
            AccountRoleRepisitory = accountRoleRepisitory;
            PermissionRepisitory = permissionRepisitory;
            RolePermissionRepisitory = rolePermissionRepisitory;
        }



        public async Task<APIResult<AccountDTO>>Login(string? AccountName,string? AccountPass)
        {
            APIResult<AccountDTO> result = new APIResult<AccountDTO>();
            result.Code = APIEnumsCode.登录成功;
            result.Message = APIEnumsCode.登录成功.ToString();

            var list = from a in AccountRepisitory.GetAll()
                       join b in AccountRoleRepisitory.GetAll() on a.AccountId equals b.AccountId
                       join c in RoleRepisitory.GetAll() on b.RoleId equals c.RoleId
                       where a.AccountName == AccountName
                       select new AccountDTO 
                       {
                           AccountId = a.AccountId,
                           AccountName = a.AccountName,
                           AccountPass = a.AccountPass,
                           AccountNickname = a.AccountNickname,
                           RoleId = c.RoleId,
                           RoleName = c.RoleName,
                       };

            if(list.Count() == 0)
            {
                result.Code = APIEnumsCode.登录失败;
                result.Message = APIEnumsCode.登录失败.ToString()+",用户名不存在";
                return await Task.FromResult(result);
            }

            var entity = list.FirstOrDefault();

            if(entity.AccountPass != AccountPass)
            {
                result.Code = APIEnumsCode.登录失败;
                result.Message = APIEnumsCode.登录失败.ToString() + ",用户名或密码不存在";
                return  await Task.FromResult(result);
            }

            result.Data = entity;
            return await Task.FromResult(result);





        }
    }
}
